package com.example.universitylibraryback.demo;

import com.example.universitylibraryback.pipeline.Pipeline;
import com.example.universitylibraryback.pipeline.ReaderRegistrationContext;
import com.example.universitylibraryback.pipeline.filter.RequestParserFilter;
import com.example.universitylibraryback.pipeline.filter.DataValidationFilter;
import com.example.universitylibraryback.pipeline.filter.NameNormalizationFilter;
import com.example.universitylibraryback.pipeline.filter.PersistenceFilter;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 读者注册流程演示类
 * 演示管道-过滤器架构在读者注册过程中的应用
 */
public class ReaderRegistrationDemo {
    public static void main(String[] args) {
        try {
            // 1. 创建管道并组装过滤器（顺序很重要！）
            Pipeline<ReaderRegistrationContext> pipeline = new Pipeline<>();
            pipeline.addFilter(new RequestParserFilter())
                    .addFilter(new DataValidationFilter())
                    .addFilter(new NameNormalizationFilter())
                    .addFilter(new PersistenceFilter());

            // 2. 模拟不同的输入数据
            String testInput1 = "R001,张三,zhangsan@example.com"; // 有效输入
            String testInput2 = "R002,李四,lisi@example";          // 无效邮箱
            String testInput3 = "X001,王五,wangwu@example.com";     // 无效读者ID
            String testInput4 = "R003,,zhaoliu@example.com";       // 空姓名
            String testInput5 = "R004,赵六";                      // 格式错误

            // 3. 创建上下文对象并提交到管道处理
            System.out.println("====== 开始处理读者注册请求 ======");
            System.out.println("提交请求1: " + testInput1);
            pipeline.submit(new ReaderRegistrationContext(testInput1));
            
            System.out.println("提交请求2: " + testInput2);
            pipeline.submit(new ReaderRegistrationContext(testInput2));
            
            System.out.println("提交请求3: " + testInput3);
            pipeline.submit(new ReaderRegistrationContext(testInput3));
            
            System.out.println("提交请求4: " + testInput4);
            pipeline.submit(new ReaderRegistrationContext(testInput4));
            
            System.out.println("提交请求5: " + testInput5);
            pipeline.submit(new ReaderRegistrationContext(testInput5));

            // 等待处理完成
            System.out.println("====== 请求已提交，等待处理完成 ======");
            TimeUnit.SECONDS.sleep(3); // 给足够时间让所有过滤器处理完请求

            // 4. 关闭管道
            pipeline.shutdown();
            System.out.println("====== 读者注册流程演示结束 ======");

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("演示过程被中断: " + e.getMessage());
        }
    }
    
    /**
     * 使用队列直接实现的读者注册流程演示
     * 模拟生产者-消费者模式
     */
    public static void demoWithQueues() {
        try {
            // 创建队列
            BlockingQueue<ReaderRegistrationContext> queue1 = new LinkedBlockingQueue<>();
            BlockingQueue<ReaderRegistrationContext> queue2 = new LinkedBlockingQueue<>();
            BlockingQueue<ReaderRegistrationContext> queue3 = new LinkedBlockingQueue<>();
            BlockingQueue<ReaderRegistrationContext> finalQueue = new LinkedBlockingQueue<>();
            
            // 创建过滤器并设置队列
            RequestParserFilter parserFilter = new RequestParserFilter();
            DataValidationFilter validationFilter = new DataValidationFilter();
            NameNormalizationFilter normalizationFilter = new NameNormalizationFilter();
            
            // 连接过滤器链
            parserFilter.process(queue1, queue2);
            validationFilter.process(queue2, queue3);
            normalizationFilter.process(queue3, finalQueue);
            
            // 3. 模拟生产者 - 主线程提交请求 
            String[] testInputs = { 
                "R001,Zhang San,zhangsan@example.com", 
                "R002,Li Si,lisi@example.com", 
                "invalid_format", 
                "R003,Wang Wu,wangwu@example.com" 
            };

            for (String input : testInputs) { 
                queue1.put(new ReaderRegistrationContext(input)); // 生产者放入初始队列 
            }

            // 4. 模拟消费者 - 主线程从最终队列取结果 
            for (int i = 0; i < testInputs.length; i++) { 
                ReaderRegistrationContext result = finalQueue.take(); 
                System.out.println("Result: " + result.isValid() + " - " + result.getMessage()); 
                // 可根据结果进行后续操作，如返回给客户端 
            }

            // 5. 关闭相关线程 
            // 在实际长期运行的服务中，需要设计更完善的关闭机制，如通过毒丸对象或中断信号通知过滤器线程退出循环。
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("演示过程被中断: " + e.getMessage());
        }
    }
}